<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form :model="lookParm"
             label-width="80px"
             :inline="true"
             size="mini">
      <el-form-item label="读者学号">
        <el-input v-model="lookParm.username"></el-input>
      </el-form-item>
      <el-form-item label="读者姓名">
        <el-input v-model="lookParm.learnNum"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="lookParm.bookName"></el-input>
      </el-form-item>
      <el-form-item label="借书状态">
        <el-select v-model="lookParm.borrowStatus"
                   placeholder="请选择">
          <el-option v-for="item in options"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="异常状态">
        <el-select v-model="lookParm.returnStatus"
                   placeholder="请选择">
          <el-option v-for="item in abnormalOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到期状态">
        <el-select v-model="lookParm.timeStatus"
                   placeholder="请选择">
          <el-option v-for="item in timeOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="lookParm.applyStatus"
                   placeholder="请选择">
          <el-option v-for="item in applyOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search"
                   @click="searchBtn">搜索</el-button>
        <el-button style="color: #ff7670"
                   icon="el-icon-close"
                   @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table size="small"
              ref="tables"
              :height="tableHeight"
              :data="tableData"
              border
              stripe>
      <el-table-column type="selection"
                       width="55"> </el-table-column>
      <el-table-column prop="bookName"
                       label="图书名称"> </el-table-column>
      <el-table-column prop="bookPlaceNum"
                       label="书架号"> </el-table-column>
      <el-table-column prop="username"
                       label="学号"> </el-table-column>
      <el-table-column prop="learnNum"
                       label="读者"> </el-table-column>
      <el-table-column prop="className"
                       label="班级"> </el-table-column>
      <el-table-column prop="phone"
                       label="电话"> </el-table-column>
      <el-table-column prop="borrowTime"
                       label="借书时间"> </el-table-column>
      <el-table-column prop="returnTime"
                       label="还书时间"> </el-table-column>
      <el-table-column prop="applyText"
                       label="审核备注"
                       align="center"> </el-table-column>
      <el-table-column prop="exceptionText"
                       label="还书备注"
                       align="center"> </el-table-column>
      <el-table-column prop="borrowStatus"
                       label="借书状态"
                       align="center"
                       width="100">
        <template slot-scope="scope">
          <el-tag type="danger"
                  v-if="scope.row.borrowStatus == '0'">待审核</el-tag>
          <el-tag v-if="scope.row.borrowStatus == '1'">在借中</el-tag>
          <el-tag type="success"
                  v-if="scope.row.borrowStatus == '2'">已还</el-tag>
          <el-tag type="danger"
                  v-if="scope.row.borrowStatus == '3'">拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="timeStatus"
                       label="到期状态"
                       align="center"
                       width="100">
        <template slot-scope="scope">
          <el-tag type="danger"
                  v-if="scope.row.timeStatus == '1'">到期</el-tag>
          <el-tag type="success"
                  v-if="scope.row.timeStatus == '0'">未到期</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="applyStatus"
                       label="审核状态"
                       align="center"
                       width="100">
        <template slot-scope="scope">
          <el-tag type="danger"
                  v-if="scope.row.applyStatus == '0'">待审核</el-tag>
          <el-tag type="success"
                  v-if="scope.row.applyStatus == '1'">已审核</el-tag>
          <el-tag type="danger"
                  v-if="scope.row.applyStatus == '2'">拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       align="center"
                       width="210">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="small"
                     @click="applyBtn(scope.row)"
                     icon="el-icon-edit"
                     v-permission="['sys:borrowLook:agree']"
                     v-if="scope.row.borrowStatus == '0'">同意借阅</el-button>
          <el-button type="primary"
                     size="small"
                     @click="addTimeBtn(scope.row)"
                     icon="el-icon-plus"
                     v-permission="['sys:borrowLook:addTime']">续期</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @size-change="sizeChange"
                   @current-change="currentChange"
                   :current-page.sync="lookParm.currentPage"
                   :page-sizes="[5, 10, 20, 40, 80, 100]"
                   :page-size="lookParm.pageSize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="lookParm.total"
                   background>
    </el-pagination>
    <!-- 借书续期弹框 -->
    <sys-dialog :title="dialog.title"
                :visible="dialog.visible"
                :width="dialog.width"
                :height="dialog.height"
                @onClose="onClose"
                @onConfirm="onConfirm">
      <div slot="content">
        <el-form :model="addTime"
                 ref="addTime"
                 :rules="rules"
                 label-width="80px"
                 :inline="false"
                 size="small">
          <el-form-item label="续期时间"
                        prop="returnTime">
            <el-date-picker v-model="addTime.returnTime"
                            type="date"
                            placeholder="选择续期日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import { getUserId } from '@/utils/auth'
import { getLookBorrowListApi, applyBookApi, addTimeApi } from '@/api/borrow'
export default {
  components: {
    SysDialog,
  },
  data() {
    return {
      //借书续期弹框表格验证规则
      rules: {
        returnTime: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择续期时间！',
          },
        ],
      },
      //借书续期弹框表格属性
      addTime: {
        returnTime: '',
        borrowId: '',
      },
      //借书续期弹框属性
      dialog: {
        title: '借书续期',
        visible: false,
        width: 650,
        height: 150,
      },
      tableHeight: 0,
      //后端的LookBorrow实体类数据->表格绑定数据
      tableData: [],
      applyOptions: [
        {
          value: '0',
          label: '待审核',
        },
        {
          value: '1',
          label: '已审核',
        },
        {
          value: '2',
          label: '拒绝',
        },
      ],
      timeOptions: [
        {
          value: '1',
          label: '到期',
        },
        {
          value: '0',
          label: '未到期',
        },
      ],
      abnormalOptions: [
        {
          value: '1',
          label: '正常还书',
        },
        {
          value: '2',
          label: '异常还书',
        },
      ],
      options: [
        {
          value: '1',
          label: '在借中',
        },
        {
          value: '2',
          label: '已还',
        },
        {
          value: '3',
          label: '拒绝',
        },
      ],
      //后端的LookParm实体类数据->搜索栏绑定数据
      lookParm: {
        userId: getUserId(),
        total: 0,
        currentPage: 1,
        pageSize: 5,
        username: '',
        learnNum: '',
        bookName: '',
        borrowStatus: '',
        returnStatus: '',
        timeStatus: '',
        applyStatus: '',
      },
      tableHeight: 0,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 250
    })
    this.getLookBorrowList()
  },
  created() {
    this.getLookBorrowList()
  },
  methods: {
    onClose() {
      this.dialog.visible = false
    },
    onConfirm() {
      this.$refs.addTime.validate(async (valid) => {
        if (valid) {
          let res = await addTimeApi(this.addTime)
          if (res && res.code == 200) {
            this.$message.success(res.msg)
            this.getLookBorrowList()
            this.dialog.visible = false
          }
        }
      })
    },
    addTimeBtn(row) {
      console.log(row)
      this.addTime.borrowId = row.borrowId
      this.dialog.visible = true
    },
    async applyBtn(row) {
      console.log(row)
      let confirm = await this.$myconfirm('确定审核吗？')
      if (confirm) {
        let res = await applyBookApi({ borrowId: row.borrowId })
        if (res && res.code == 200) {
          this.$message.success(res.msg)
          this.getLookBorrowList()
        }
      }
    },
    resetBtn() {
      this.lookParm.currentPage = 1
      this.lookParm.username = ''
      this.lookParm.learnNum = ''
      this.lookParm.bookName = ''
      this.lookParm.borrowStatus = ''
      this.lookParm.returnStatus = ''
      this.lookParm.timeStatus = ''
      this.lookParm.applyStatus = ''
      this.getLookBorrowList()
    },
    searchBtn() {
      this.getLookBorrowList()
    },
    currentChange(val) {
      this.lookParm.currentPage = val
      this.getLookBorrowList()
    },
    sizeChange(val) {
      this.lookParm.pageSize = val
      this.getLookBorrowList()
    },
    async getLookBorrowList() {
      let res = await getLookBorrowListApi(this.lookParm)
      if (res && res.code == 200) {
        this.tableData = res.data.records
        this.lookParm.total = res.data.total
      }
    },
  },
}
</script>

<style lang="scss" scoped>
</style>